home *** CD-ROM | disk | FTP | other *** search
- { *****************************************************
- HomeGrown's 'Expert' Expert
-
- This expert is designed to create Standard, Form and
- Project experts with or without a Form.
-
- If you choose to generate a form it will produce a
- plain form since it is hard to guess what a given
- expert will need on a form.
-
- In addition, I chose to generate overrides for all
- TIExpert methods even though you will not need them
- all. This was done for simplicity. Remove any
- overrides you don't need for a given expert type.
-
- That being said this expert generator will give you
- a working expert with very little work.
-
- Note: This unit is for Delphi 2.0. Change the
- indicated lines to compile in Delphi 1.0. I couldn't
- use conditional defines since proxies.dcu is
- different in Delphi 2.0.
-
- Enjoy!
-
- Paul Warren
- HomeGrown Software Development
- (c) 1996 Langley British Columbia.
- (604) 530-9097
- e-mail: hg_soft@uniserve.com
- Home page: http://haven.uniserve.com/~hg_soft
- ***************************************************** }
-
- unit exptexp;
-
- interface
-
- uses
- SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
- Forms, Dialogs, StdCtrls, Buttons, ExtCtrls, ExptIntf, ToolIntf;
-
- type
- { These are the set of flags which determine the type of expert to create }
- TExpAttr = (eaStandard, eaForm, eaProject, eaCreateForm);
- TExpAttrs = set of TExpAttr;
-
- ThgExpExpert = class(TForm)
- Bevel1: TBevel;
- BitBtn1: TBitBtn;
- BitBtn2: TBitBtn;
- Label1: TLabel;
- BitBtn3: TBitBtn;
- Memo1: TMemo;
- Label4: TLabel;
- Label5: TLabel;
- Memo2: TMemo;
- GroupBox1: TGroupBox;
- cbStandard: TCheckBox;
- cbForm: TCheckBox;
- cbProject: TCheckBox;
- GroupBox2: TGroupBox;
- Label2: TLabel;
- Edit2: TEdit;
- cbMakeForm: TCheckBox;
- Edit1: TEdit;
- procedure StyleClick(Sender: TObject);
- procedure BitBtn2Click(Sender: TObject);
- procedure FormShow(Sender: TObject);
- procedure BitBtn3Click(Sender: TObject);
- procedure BitBtn1Click(Sender: TObject);
- procedure Edit1KeyPress(Sender: TObject; var Key: Char);
- procedure Edit2KeyPress(Sender: TObject; var Key: Char);
- private
- { Private declarations }
- SourceBuffer: PChar;
- Definition: TExpAttrs;
- procedure FmtWrite(Stream: TStream; Fmt: PChar; const Args: array of const);
- function CheckOKToRun: boolean;
- function DoFormCreation(const FormIdent: string): TForm;
- function CreateSource(const UnitIdent, FormIdent: string): TMemoryStream;
- function CreateForm(const FormIdent: string): TMemoryStream;
- public
- { Public declarations }
- end;
-
- ThgExpertExpert = class(TIExpert)
- public
- function GetStyle: TExpertStyle; override;
- function GetIDString: string; override;
- function GetName: string; override;
- function GetComment: string; override;
- function GetGlyph: HBITMAP; override;
- function GetState: TExpertState; override;
- function GetMenuText: string; override;
- procedure Execute; override;
- end;
-
- var
- hgExpExpert: ThgExpExpert;
-
- procedure Register;
-
- implementation
-
- uses VirtIntf, IStreams, Proxies;
-
- {$R *.DFM}
-
- const
- SourceBufferSize = 2048;
-
- { FormShow method - populate memo and set defaults }
- procedure ThgExpExpert.FormShow(Sender: TObject);
- begin
- { load copyright info }
- Memo2.Lines.LoadFromFile('\WINDOWS\CPYRIGHT.TXT');
- { include eaStandard and eaCreateForm in Definition - default }
- Include(Definition, eaStandard);
- Include(Definition, eaCreateForm);
- end;
-
- { StyleClick method - change the style flags }
- procedure ThgExpExpert.StyleClick(Sender: TObject);
- begin
- if cbStandard.Checked then Include(Definition, eaStandard)
- else Exclude(Definition, eaStandard);
- if cbForm.Checked then Include(Definition, eaForm)
- else Exclude(Definition, eaForm);
- if cbProject.Checked then Include(Definition, eaProject)
- else Exclude(Definition, eaProject);
- if cbMakeForm.Checked then Include(Definition, eaCreateForm)
- else Exclude(Definition, eaCreateForm);
- end;
-
- { BitBtn3Click method - display message }
- procedure ThgExpExpert.BitBtn3Click(Sender: TObject);
- begin
- MessageDlg('This expert, by HomeGrown Software Development, will'#13+
- 'create a new expert outline and add comments and'#13+
- 'copyright information.', mtInformation, [mbOk], 0);
- end;
-
- { FmtWrite method - write formatted strings to the SourceBuffer }
- procedure ThgExpExpert.FmtWrite(Stream: TStream; Fmt: PChar;
- const Args: array of const);
- begin
- if (Stream <> nil) and (SourceBuffer <> nil) then
- begin
- StrLFmt(SourceBuffer, SourceBufferSize, Fmt, Args);
- Stream.Write(SourceBuffer[0], StrLen(SourceBuffer));
- end;
- end;
-
- { CreateSource method - write the synchronous source to SourceBuffer. }
- function ThgExpExpert.CreateSource(const UnitIdent, FormIdent: string): TMemoryStream;
- const
- CRLF = #13#10;
- var
- i: integer;
- begin
- SourceBuffer := StrAlloc(SourceBufferSize);
- try
- Result := TMemoryStream.Create;
- try
- FmtWrite(Result,
- '{ *****************************************************'+CRLF+
- ' %s Expert'+CRLF+CRLF, [Edit1.Text]);
-
- for i := 0 to Memo1.Lines.Count-1 do
- begin
- FmtWrite(Result, ' %s'+CRLF, [Memo1.Lines[i]]);
- end;
-
- FmtWrite(Result, CRLF, [nil]);
-
- for i := 0 to Memo2.Lines.Count-1 do
- begin
- FmtWrite(Result, ' %s'+CRLF, [Memo2.Lines[i]]);
- end;
-
- FmtWrite(Result,
- ' ***************************************************** }'+CRLF+CRLF+
- 'unit %s;'+CRLF+CRLF+
- 'interface'+CRLF+CRLF, [UnitIdent]);
-
- FmtWrite(Result,
- 'uses'+CRLF+
- ' SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,'+CRLF+
- ' Forms, Dialogs, ExptIntf, ToolIntf;'+CRLF+CRLF, [nil]);
-
- { begin the class declaration }
- if eaCreateForm in Definition then
- FmtWrite(Result,
- 'type'+CRLF+
- ' T%s = class(TForm)'+CRLF+
- ' end;'+CRLF+CRLF, [FormIdent]);
-
- { Standard Expert }
- if eaStandard in Definition then
- begin
- FmtWrite(Result,
- 'type'+CRLF+
- ' T%sStandardExpert = class(TIExpert)'+CRLF+
- ' public'+CRLF+
- ' function GetStyle: TExpertStyle; override;'+CRLF+
- ' function GetIDString: string; override;'+CRLF+
- ' function GetName: string; override;'+CRLF, [Edit1.Text]);
-
- FmtWrite(Result,
- ' function GetComment: string; override;'+CRLF+
- ' function GetGlyph: HICON; override;'+CRLF+ {change HICON to HBITMAP for Delphi 1.0}
- ' function GetState: TExpertState; override;'+CRLF+
- ' function GetMenuText: string; override;'+CRLF, [nil]);
-
- FmtWrite(Result,
- ' procedure Execute; override;'+CRLF+
- ' end;'+CRLF+CRLF, [nil]);
- end;
-
- { Form Expert }
- if eaForm in Definition then
- begin
- FmtWrite(Result,
- 'type'+CRLF+
- ' T%sFormExpert = class(TIExpert)'+CRLF+
- ' public'+CRLF+
- ' function GetStyle: TExpertStyle; override;'+CRLF+
- ' function GetIDString: string; override;'+CRLF+
- ' function GetName: string; override;'+CRLF, [Edit1.Text]);
-
- FmtWrite(Result,
- ' function GetComment: string; override;'+CRLF+
- ' function GetGlyph: HICON; override;'+CRLF+ {change HICON to HBITMAP for Delphi 1.0}
- ' function GetState: TExpertState; override;'+CRLF+
- ' function GetMenuText: string; override;'+CRLF, [nil]);
-
- FmtWrite(Result,
- ' procedure Execute; override;'+CRLF+
- ' end;'+CRLF+CRLF, [nil]);
- end;
-
- { Project Expert }
- if eaProject in Definition then
- begin
- FmtWrite(Result,
- 'type'+CRLF+
- ' T%sProjectExpert = class(TIExpert)'+CRLF+
- ' public'+CRLF+
- ' function GetStyle: TExpertStyle; override;'+CRLF+
- ' function GetIDString: string; override;'+CRLF+
- ' function GetName: string; override;'+CRLF, [Edit1.Text]);
-
- FmtWrite(Result,
- ' function GetComment: string; override;'+CRLF+
- ' function GetGlyph: HICON; override;'+CRLF+ {change HICON to HBITMAP for Delphi 1.0}
- ' function GetState: TExpertState; override;'+CRLF+
- ' function GetMenuText: string; override;'+CRLF, [nil]);
-
- FmtWrite(Result,
- ' procedure Execute; override;'+CRLF+
- ' end;'+CRLF+CRLF, [nil]);
- end;
-
- FmtWrite(Result,
- 'procedure Register;'+CRLF+CRLF, [nil]);
-
- if eaCreateForm in Definition then
- FmtWrite(Result,
- 'var' + CRLF +
- ' %s: T%s;'+CRLF+CRLF, [FormIdent, FormIdent]);
-
- FmtWrite(Result,
- 'implementation'+CRLF+CRLF, [nil]);
-
- FmtWrite(Result,
- 'uses VirtIntf, IStreams;'+CRLF+CRLF+
- '{$R *.DFM}'+CRLF+CRLF+
- 'const'+CRLF+
- ' SourceBufferSize = 1024;'+CRLF+CRLF, [nil]);
-
- FmtWrite(Result,
- 'procedure Register;'+CRLF+
- 'begin'+CRLF, [nil]);
-
- if eaStandard in Definition then
- FmtWrite(Result,
- ' RegisterLibraryExpert(T%sStandardExpert.Create);'+CRLF, [Edit1.Text]);
-
- if eaForm in Definition then
- FmtWrite(Result,
- ' RegisterLibraryExpert(T%sFormExpert.Create);'+CRLF, [Edit1.Text]);
-
- if eaProject in Definition then
- FmtWrite(Result,
- ' RegisterLibraryExpert(T%sProjectExpert.Create);'+CRLF, [Edit1.Text]);
-
- FmtWrite(Result,
- 'end;'+CRLF+CRLF, [nil]);
-
- { Standard Expert }
- if eaStandard in Definition then
- begin
- FmtWrite(Result,
- '{ T%sStandardExpert code }'+CRLF+
- 'function T%sStandardExpert.GetStyle: TExpertStyle;'+CRLF+
- 'begin'+CRLF+
- ' Result := esStandard;'+CRLF+
- 'end;'+CRLF+CRLF, [Edit1.Text, Edit1.Text]);
-
- FmtWrite(Result,
- 'function T%sStandardExpert.GetIDString: String;'+CRLF+
- 'begin'+CRLF+
- ' Result := ''%s.%sStandardExpert'';'+CRLF+
- 'end;'+CRLF+CRLF+
- 'function T%sStandardExpert.GetComment: String;'+CRLF+
- 'begin'+CRLF+
- ' Result := ''''; { not needed for esStandard }'+CRLF+
- 'end;'+CRLF+CRLF, [Edit1.Text, Edit2.Text, Edit1.Text, Edit1.Text]);
-
- FmtWrite(Result,
- 'function T%sStandardExpert.GetGlyph: HICON;'+CRLF+ {change HICON to HBITMAP for Delphi 1.0}
- 'begin'+CRLF+
- ' Result := 0; { not needed for esStandard }'+CRLF+
- 'end;'+CRLF+CRLF+
- 'function T%sStandardExpert.GetName: String;'+CRLF+
- 'begin'+CRLF+
- ' Result := ''%s Generator'''+CRLF+
- 'end;'+CRLF+CRLF, [Edit1.Text, Edit1.Text, Edit1.Text]);
-
- FmtWrite(Result,
- 'function T%sStandardExpert.GetState: TExpertState;'+CRLF+
- 'begin'+CRLF+
- ' Result := [esEnabled]'+CRLF+
- 'end;'+CRLF+CRLF+
- 'function T%sStandardExpert.GetMenuText: String;'+CRLF+
- 'begin'+CRLF+
- ' Result := ''%s Standard Expert...'''+CRLF+
- 'end;'+CRLF+CRLF, [Edit1.Text, Edit1.Text, Edit1.Text]);
-
- FmtWrite(Result,
- 'procedure T%sStandardExpert.Execute;'+CRLF+
- 'begin'+CRLF, [Edit1.Text]);
-
- if eaCreateForm in Definition then
- FmtWrite(Result,
- ' if not Assigned(%s) then'+CRLF+
- ' %s := T%s.Create(Application);'+CRLF+
- ' %s.Show;'+CRLF+
- ' %s.SetFocus'+CRLF,[FormIdent, FormIdent,
- FormIdent, FormIdent, FormIdent]);
-
- FmtWrite(Result,
- 'end;'+CRLF+CRLF,[nil]);
- end;
-
- { Form Expert }
- if eaForm in Definition then
- begin
- FmtWrite(Result,
- '{ T%sFormExpert code }'+CRLF+
- 'function T%sFormExpert.GetStyle: TExpertStyle;'+CRLF+
- 'begin'+CRLF+
- ' Result := esForm;'+CRLF+
- 'end;'+CRLF+CRLF, [Edit1.Text, Edit1.Text]);
-
- FmtWrite(Result,
- 'function T%sFormExpert.GetIDString: String;'+CRLF+
- 'begin'+CRLF+
- ' Result := ''%s.%sFormExpert'';'+CRLF+
- 'end;'+CRLF+CRLF+
- 'function T%sFormExpert.GetComment: String;'+CRLF+
- 'begin'+CRLF+
- ' Result := ''Generated by HomeGrown''''s ''''Expert'''' expert.'';'+CRLF+
- 'end;'+CRLF+CRLF, [Edit1.Text, Edit2.Text, Edit1.Text, Edit1.Text]);
-
- FmtWrite(Result,
- 'function T%sFormExpert.GetGlyph: HICON;'+CRLF+ {change HICON to HBITMAP for Delphi 1.0}
- 'begin'+CRLF+
- ' Result := 0;'+CRLF+
- 'end;'+CRLF+CRLF+
- 'function T%sFormExpert.GetName: String;'+CRLF+
- 'begin'+CRLF+
- ' Result := ''%s Generator'''+CRLF+
- 'end;'+CRLF+CRLF, [Edit1.Text, Edit1.Text, Edit1.Text]);
-
- FmtWrite(Result,
- 'function T%sFormExpert.GetState: TExpertState;'+CRLF+
- 'begin'+CRLF+
- ' Result := [esEnabled]'+CRLF+
- 'end;'+CRLF+CRLF+
- 'function T%sFormExpert.GetMenuText: String;'+CRLF+
- 'begin'+CRLF+
- ' Result := ''%s Form Expert...'''+CRLF+
- 'end;'+CRLF+CRLF, [Edit1.Text, Edit1.Text, Edit1.Text]);
-
- FmtWrite(Result,
- 'procedure T%sFormExpert.Execute;'+CRLF+
- 'begin'+CRLF, [Edit1.Text]);
-
- if eaCreateForm in Definition then
- FmtWrite(Result,
- ' if not Assigned(%s) then'+CRLF+
- ' %s := T%s.Create(Application);'+CRLF+
- ' %s.Show;'+CRLF+
- ' %s.SetFocus'+CRLF,[FormIdent, FormIdent,
- FormIdent, FormIdent, FormIdent]);
-
- FmtWrite(Result,
- 'end;'+CRLF+CRLF,[nil]);
- end;
-
- { Project Expert }
- if eaProject in Definition then
- begin
- FmtWrite(Result,
- '{ T%sProjectExpert code }'+CRLF+
- 'function T%sProjectExpert.GetStyle: TExpertStyle;'+CRLF+
- 'begin'+CRLF+
- ' Result := esProject;'+CRLF+
- 'end;'+CRLF+CRLF, [Edit1.Text, Edit1.Text]);
-
- FmtWrite(Result,
- 'function T%sProjectExpert.GetIDString: String;'+CRLF+
- 'begin'+CRLF+
- ' Result := ''%s.%sProjectExpert'';'+CRLF+
- 'end;'+CRLF+CRLF+
- 'function T%sProjectExpert.GetComment: String;'+CRLF+
- 'begin'+CRLF+
- ' Result := ''Generated by HomeGrown''''s ''''Expert'''' expert.'';'+CRLF+
- 'end;'+CRLF+CRLF, [Edit1.Text, Edit2.Text, Edit1.Text, Edit1.Text]);
-
- FmtWrite(Result,
- 'function T%sProjectExpert.GetGlyph: HICON;'+CRLF+ {change HICON to HBITMAP for Delphi 1.0}
- 'begin'+CRLF+
- ' Result := 0;'+CRLF+
- 'end;'+CRLF+CRLF+
- 'function T%sProjectExpert.GetName: String;'+CRLF+
- 'begin'+CRLF+
- ' Result := ''%s Generator'''+CRLF+
- 'end;'+CRLF+CRLF, [Edit1.Text, Edit1.Text, Edit1.Text]);
-
- FmtWrite(Result,
- 'function T%sProjectExpert.GetState: TExpertState;'+CRLF+
- 'begin'+CRLF+
- ' Result := [esEnabled]'+CRLF+
- 'end;'+CRLF+CRLF+
- 'function T%sProjectExpert.GetMenuText: String;'+CRLF+
- 'begin'+CRLF+
- ' Result := ''%s ProjectExpert...'''+CRLF+
- 'end;'+CRLF+CRLF, [Edit1.Text, Edit1.Text, Edit1.Text]);
-
- FmtWrite(Result,
- 'procedure T%sProjectExpert.Execute;'+CRLF+
- 'begin'+CRLF, [Edit1.Text]);
-
- if eaCreateForm in Definition then
- FmtWrite(Result,
- ' if not Assigned(%s) then'+CRLF+
- ' %s := T%s.Create(Application);'+CRLF+
- ' %s.Show;'+CRLF+
- ' %s.SetFocus'+CRLF,[FormIdent, FormIdent,
- FormIdent, FormIdent, FormIdent]);
-
- FmtWrite(Result,
- 'end;'+CRLF+CRLF,[nil]);
- end;
-
- if eaCreateForm in Definition then
- FmtWrite(Result,
- '{ T%s code }'+CRLF+CRLF, [FormIdent]);
-
- FmtWrite(Result,
- 'end.', [nil]);
-
- Result.Position := 0;
- except
- Result.Free;
- raise;
- end;
- finally
- StrDispose(SourceBuffer);
- end;
- end;
-
- { DoFormCreation method - Create the dialog defined by the user }
- function ThgExpExpert.DoFormCreation(const FormIdent: string): TForm;
- begin
- { remove the comments from the next line to compile in Delphi 1.0 }
- {Result := TProxyForm.CreateAs('T' + FormIdent);}
- { comment out the next 2 lines to compile in Delphi 1.0 }
- Result := TForm.Create(nil);
- Proxies.CreateSubClass(Result, 'T' + FormIdent, TForm);
- with Result do
- begin
- BorderStyle := bsSizeable;
- Width := 400;
- Height := 250;
- Position := poScreenCenter;
- Name := FormIdent;
- Caption := FormIdent;
- end;
- end;
-
- { CreateForm method - create the form, write it out to disk }
- function ThgExpExpert.CreateForm(const FormIdent: string): TMemoryStream;
- var
- NewForm: TForm;
- begin
- Result := nil;
- NewForm := DoFormCreation(FormIdent);
- try
- Result := TMemoryStream.Create;
- Result.WriteComponentRes(FormIdent, NewForm);
- Result.Position := 0;
- finally
- NewForm.Free;
- end;
- end;
-
- function ThgExpExpert.CheckOKToRun: boolean;
- begin
- if (Edit1.Text <> '') and (Edit2.Text <> '') and
- (cbStandard.Checked or cbForm.Checked or cbProject.Checked) then
- Result := true
- else Result := false;
- end;
-
- { BitBtn1Click method - This method does the actual generating. Note the
- check for ToolServices <> nil. This guarantees the library is running. }
- procedure ThgExpExpert.BitBtn1Click(Sender: TObject);
- var
- FileName: TFileName;
- ISourceStream, IFormStream: TIMemoryStream;
- UnitIdent, FormIdent: string;
- begin
- if CheckOKToRun then
- begin
- if ToolServices <> nil then { I'm an expert!! }
- begin
- if ToolServices.GetNewModuleName(UnitIdent, FileName) then
- try
- UnitIdent := LowerCase(UnitIdent);
- UnitIdent[1] := Upcase(UnitIdent[1]);
- FormIdent := 'Form' + Copy(UnitIdent, 5, 255);
- if eaCreateForm in Definition then
- begin
- IFormStream := TIMemoryStream.Create(CreateForm(FormIdent));
- { remove or comment out the next line to compile in Delphi 1.0. }
- IFormStream.AddRef;
- ISourceStream := TIMemoryStream.Create(CreateSource(UnitIdent, FormIdent));
- end else
- ISourceStream := TIMemoryStream.Create(CreateSource(UnitIdent, ''));
- try
- { remove or comment out the next line to compile in Delphi 1.0. }
- ISourceStream.AddRef;
- if eaCreateForm in Definition then
- ToolServices.CreateModule(FileName, ISourceStream, IFormStream,
- [cmShowSource, cmShowForm, cmUnNamed, cmMarkModified])
- else
- ToolServices.CreateModule(FileName, ISourceStream, nil,
- [cmShowSource, cmUnNamed, cmMarkModified]);
- finally
- ISourceStream.OwnStream := True;
- ISourceStream.Free;
- end;
- Close;
- finally
- if eaCreateForm in Definition then
- begin
- IFormStream.OwnStream := True;
- IFormStream.Free;
- end;
- end;
- end;
- end;
- end;
-
- { BitBtn2Click method - close expert }
- procedure ThgExpExpert.BitBtn2Click(Sender: TObject);
- begin
- Close;
- end;
-
- procedure ThgExpExpert.Edit1KeyPress(Sender: TObject; var Key: Char);
- begin
- if Key = ' ' then Key := '_';
- end;
-
- procedure ThgExpExpert.Edit2KeyPress(Sender: TObject; var Key: Char);
- begin
- if Key = ' ' then Key := '_';
- end;
-
- { TIExpert override methods }
- function ThgExpertExpert.GetStyle: TExpertStyle;
- begin
- { it's a standard expert }
- Result := esStandard;
- end;
-
- function ThgExpertExpert.GetIDString: String;
- begin
- { unique ID string }
- Result := 'hgsoft.ExpertExpert';
- end;
-
- function ThgExpertExpert.GetComment: String;
- begin
- Result := ''; { not needed for esStandard }
- end;
-
- function ThgExpertExpert.GetGlyph: HBITMAP;
- begin
- Result := 0; { not needed for esStandard }
- end;
-
- function ThgExpertExpert.GetName: String;
- begin
- Result := 'Expert Generator';
- end;
-
- function ThgExpertExpert.GetState: TExpertState;
- begin
- Result := [esEnabled];
- end;
-
- function ThgExpertExpert.GetMenuText: String;
- begin
- Result := 'E&xpert Expert...';
- end;
-
- procedure ThgExpertExpert.Execute;
- begin
- if not Assigned(hgExpExpert) then
- hgExpExpert := ThgExpExpert.Create(Application);
- hgExpExpert.ShowModal;
- end;
-
- { register the 'Expert' expert }
- procedure Register;
- begin
- RegisterLibraryExpert(ThgExpertExpert.Create);
- end;
-
- end.
-